Ultrasonic Distance Measurement with Arduino

This tutorial will guide you through using an ultrasonic sensor to measure distance and display the results in both centimeters and inches using an Arduino. We'll use the HC-SR04 ultrasonic sensor for this project.

Understanding Sound and Ultrasonic Waves

Humans can hear sounds within the range of 20 dB to 20,000 dB. Sounds below 20 dB are too faint for human ears, and sounds above 20,000 dB are beyond human hearing capacity.

Ultrasonic waves are sound waves with frequencies above 20 kHz (20,000 Hz), making them inaudible to humans. Bats use ultrasonic waves for echolocation, allowing them to navigate in the dark by emitting high-frequency sounds and interpreting the echoes. Inspired by bats, ultrasonic technology is widely used in applications such as SONAR (used in submarines) and RADAR (used for detecting objects at a distance).

How Ultrasonic Sensors Work

Ultrasonic sensors like the HC-SR04 use high-frequency sound waves to measure distances. The sensor emits an ultrasonic pulse that travels through the air and reflects off an object. The time taken for the echo to return is measured and converted into a distance reading using the speed of sound.

Components Needed:

  1. Arduino Board (Uno, Nano, etc.)
  2. HC-SR04 Ultrasonic Sensor
  3. Breadboard
  4. Jumper Wires
  5. USB Cable (to connect Arduino to your computer)

Step-by-Step Guide

Step 1: Circuit Setup

  1. Connect the HC-SR04 Sensor to the Arduino:
  1. (https://www.arduino.cc/en/uploads/Tutorial/Arduino-UltraSonicSensor_bb.png)


Step 2: Arduino Code

  1. Open the Arduino IDE on your computer.
  2. Copy and paste the following code into the IDE:

const int trigPin = 11;  // Trigger pin

const int echoPin = 12;  // Echo pin

long duration; // Duration of the received pulse

float distanceCm; // Distance in centimeters

float distanceInches; // Distance in inches

void setup() {

  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

}

void loop() {

  digitalWrite(trigPin, LOW);

  delayMicroseconds(5);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distanceCm = (duration * 0.0343) / 2;

  distanceInches = (duration * 0.0135) / 2;

  Serial.print("Distance: ");

  Serial.print(distanceCm);

  Serial.print(" cm, ");

  Serial.print(distanceInches);

  Serial.println(" inches");

 

  delay(100);

}

Alternative Code with LEDs (and buzzer)

If you want to use LEDs to indicate distance:

const int trigPin = 11;

const int echoPin = 12;

const int led1 = 8;

const int led2 = 9;

long duration;

float distance;

void setup() {

  pinMode(trigPin, OUTPUT);

  pinMode(echoPin, INPUT);

  pinMode(led1, OUTPUT);

  pinMode(led2, OUTPUT);

  Serial.begin(9600);

  Serial.println("Testing Ultrasonic Sensor");

}

void loop() {

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration * 0.034) / 2;

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");

  if (distance > 50) {

    digitalWrite(led1, HIGH);

    digitalWrite(led2, LOW);

  } else {

    digitalWrite(led2, HIGH);

    digitalWrite(led1, LOW);

  }

  delay(100);

}

Step 3: Code Explanation

Step 4: Upload the Code

  1. Connect your Arduino to the computer using the USB cable.
  2. Select the correct board and port in the Arduino IDE:
  1. Click the upload button (right arrow icon) to upload the code.

Step 5: View the Results

  1. Open the Serial Monitor in the Arduino IDE by clicking on the magnifying glass icon in the top right corner or by going to Tools > Serial Monitor.
  2. Set the baud rate to 9600 in the Serial Monitor.
  3. Observe the distance measurements in centimeters and inches being displayed.

Conclusion

You have successfully set up an ultrasonic distance measurement system using an Arduino and an HC-SR04 sensor. Inspired by bats and used in SONAR and RADAR systems, ultrasonic technology is widely implemented in robotics, automation, and navigation. This project can be expanded for various applications such as obstacle detection, distance measurement, and automation systems. Experiment with different setups and see what you can create!